
function soapWrapFunc(bdStr){
    // 封装SOAP头尾
    var soapHead = '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://Webservice.zjusc.edu.cn/"><env:Body>', soapTail = '</env:Body></env:Envelope>';
    return soapHead + bdStr + soapTail;
}

function loadNotice(){
	var dateReg = /\d{4}\-\d{2}\-\d{2}/;
	var str='<web:readNotice></web:readNotice>';
	
	$.ajax({
        type: "POST",
        contentType: "text/xml",
        url: '../../Service/SystemNoticeInfoService',
        data: soapWrapFunc(str),
        dataType: 'xml',
		success:function(xml){
			$.each($(xml).find("return"), function(i, n){
				//$("#StuMajor").append("<option value="+$(this).find("ma_id").text()+">"+$(this).find("ma_majorName").text()+"</option>"); 
            	$('#noticeTitle').html($(this).find('title').text());
				$('#noticeInfo').html($(this).find("publishTime").text());
				$('#content').html($(this).find("content").text().replace(new RegExp("ugly","g"),"&nbsp;"));
			});
			
		},
		beforeSend: function(xhr){
            xhr.setRequestHeader("SOAPAction", "\"\"");
        },
        error: function(x, e){
            alert('error:' + x.responseText);
        },
        complete: function(x){
        
        }
	});
}

$(function(){
	//拖拽绑定
	$('#examWnd').dragDrop({ focuEle: '#wndTitle' });
	
    $('.timezone').datePicker({
        startDate: '2000-01-01',
        endDate: '2100-01-01',
        clickInput: true,
        verticalOffset: 35
    });
    
	loadNotice();
	$('#modButton').click(function(){
        $('#maskLayer').show();
        $('#examWnd').show();
        $('#wndTitle span').text('编辑公告信息');
        var dateReg = /\d{4}\-\d{2}\-\d{2}/;
    	var str='<web:readNotice></web:readNotice>';
    	
    	$.ajax({
            type: "POST",
            contentType: "text/xml",
            url: '../../Service/SystemNoticeInfoService',
            data: soapWrapFunc(str),
            dataType: 'xml',
    		success:function(xml){
    			$.each($(xml).find("return"), function(i, n){

    				$('#inClassRoomIndex').val($(this).find('title').text());
    				$('#inExamDate').val($(this).find("publishTime").text());
    				document.getElementById("editorIFrame").contentWindow.document.body.innerHTML=$(this).find('content').text().replace(new RegExp("ugly","g"),"&nbsp;");
    			});
    			
    		},
    		beforeSend: function(xhr){
                xhr.setRequestHeader("SOAPAction", "\"\"");
            },
            error: function(x, e){
                alert('error:' + x.responseText);
            },
            complete: function(x){
            
            }
    	});
        
        
//        $("#inClassRoomIndex").val($("#noticeTitle").val());
//        $("#inExamDate").val($("#noticeInfo").val());
      
        //$("#inClassRoomIndex").val($("#noticeTitle"));
	 });
	 $('#closeBtn').click(function(){
	        $('#maskLayer').hide();
	        $('#examWnd').find('input[type="text"]').val('').end().hide();
	    });
	 
	 $('#cancelButton').click(function(){
		 
	        $('#maskLayer').hide();
	        $('#examWnd').find('input[type="text"]').val('').end().hide();
		 
	    });

	 
	 //修改公告
	 $("#confirmButton").click(function(){

		//验证报文
		    var userinfo = '<uc><authCode>' + Tr0j4n.Cookie.Get('userCode') + '</authCode><userName>' + Tr0j4n.Cookie.Get('userName') + '</userName></uc>';
		    
		 //获取公告标题
		 //var noticeTitle = $("#inClassRoomIndex").text();  //$.trim($('#year').val());
		 var noticeTitle=$.trim($('#inClassRoomIndex').val());


		 //获取公告时间
		 var publishTime = $.trim($('#inExamDate').val());

		 //获取公告内容
		 var content=document.getElementById("editorIFrame").contentWindow.document.body.innerHTML.replace(new RegExp("<","g"),"&lt;").replace(new RegExp(">","g"),"&gt;").replace(new RegExp("&nbsp;","g"),"ugly");

		 
		 var sendBody='<web:updateNotice>'+userinfo+'<sni>'+
			'<title>'+noticeTitle+'</title>'+
			'<publishTime>'+publishTime+'</publishTime>'+
			'<content>'+content+'</content>'+
			'</sni></web:updateNotice>';
		 
		 $.ajax({
	            type: "POST",
	            contentType: "text/xml;charset=UTF-8",
	            url: '../../Service/SystemNoticeInfoService',
	            data: soapWrapFunc(sendBody),
	            dataType: 'xml',
	            success: function(xml){
	                alert($(xml).find('return').text());
	                loadNotice();
	                
	            },
	            beforeSend: function(xhr){
	                xhr.setRequestHeader("SOAPAction", "\"\"");
	            },
	            error: function(x, e){
	            	 var xmlObj = $(x.responseText);
	                 if (xmlObj.find('faultstring').text() == 'oops') {
	                     alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
	                     return;
	                 }
	                 else {
	                     alert('error:' + x.responseText);
	                 }
	            },
	            complete: function(x){
	            
	            }
	        });
	 });
	 
});
